const db = wx.cloud.database({
  env: 'yun-39dfi'
});
Page({
  data: {
    name: '',
    i: 0,
    sum:0,
    id:""
  },
  onLoad: function(options) {

    var note = wx.getStorageSync('note')
    var i = this.data.i
    var id = this.data.id
    var name = this.data.name
    var sum = this.data.sum

    wx.setNavigationBarTitle({
      title: '订单详情'
    })
    db.collection('order').doc(options.order_id).get({
      success: res => {
        // console.log(res)
        var foods = res.data.cartList
        //算总价
        // var sum = 0;
        for (i in foods) {
          sum += foods[i].price * foods[i].number
        }
        if (sum >= 50 && res.data.promotion.discount > 0) {
          sum -= res.data.promotion.discount
        }
        this.setData({
          order: res.data,
          sumMonney: sum,
          sum:sum,
          note: note,
          i: i,
          id:options.order_id
        })
      }
    });
    // db.collection('order').doc(options.order_id).update({
    //   data: {   
    //     sumMonney: this.data.sum,
    //   }, success: res => {
    //   }
    // })
  },
  onUnload: function() {
    var app = getApp();
    // 支付成功之后调到订单页面，通知订单页刷新
    app.isReloadOrderList = true
    wx.switchTab({
      url: '/pages/order/list/list'
    })
  },
  qucan:function(e){
    var id = this.data.id
    db.collection("order").doc(id).update({
      data: {
        taken:true
      },
      success: res => {
        console.log(res);
        wx.switchTab({
          url: '/pages/order/list/list'
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  }
})